package com.woniuxy.service.impl;

import com.woniuxy.dao.PaperDao;
import com.woniuxy.dao.QuestionDao;
import com.woniuxy.entity.PaperAttribute;
import com.woniuxy.entity.PaperObject;
import com.woniuxy.entity.Question;
import com.woniuxy.entity.ResponseEntity;
import com.woniuxy.service.PaperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * @author:deng
 * @time:2022/4/26 17:27
 * @描述：关于试卷的service
 */
@Service
public class PaperServiceImpl implements PaperService {

    @Autowired(required = false)
    PaperDao paperDao;
    @Autowired
    QuestionDao questionDao;
    /*添加试卷信息*/
    @Override
    public ResponseEntity addPaperAttribute(PaperAttribute paperAttribute) {

        int i = paperDao.addPaperAttribute(paperAttribute);
        if (i > 0) {
            return ResponseEntity.SUCCESSFUL;
        }
        return ResponseEntity.FAIL;
    }

    /*根据题目不同的类别查询不同的题*/
    @Override
    public ResponseEntity findQuestion(int questionId) {
        int newId = paperDao.findNewId();

        List<Question> question = paperDao.findQuestion(questionId,newId);
        if (question !=null) {
            return new ResponseEntity(200,"查询成功",question);
        }
        return ResponseEntity.FAIL;
    }
    @Override
    public ResponseEntity findAllQuestionBytitle(int questionId, String title) {
        int newId = paperDao.findNewId();

        List<Question> question = paperDao.findAllQuestionBytitle(questionId,newId,title);
        if (question !=null) {
            return new ResponseEntity(200,"查询成功",question);
        }
        return ResponseEntity.FAIL;
    }

    /*根据数组(题目id)插入题数据到试卷表*/
    @Override
    public ResponseEntity addPaper(List<Integer> question) {
        //处理数据

        for (int i:question
             ) {
            System.out.println(i);
        }
        //先查询到题
        List<Question> questionslist = questionDao.findPaperAttByid(question);

        //查询添加的试卷的id
        int newId = paperDao.findNewId();
        System.out.println(newId);
        //再将提存入试卷
        int i=paperDao.addPaperByQuestion(questionslist,newId);
        if (i > 0) {
          return ResponseEntity.SUCCESSFUL;

        }
        return ResponseEntity.FAIL;
    }


    /*查询所有的题目组装好的*/
    @Override
    public ResponseEntity findAllTopic(int paperId) {
        List<PaperObject> paperObjectList=paperDao.findAllTopic(paperId);
//        非选择题
        List<PaperObject> allTopicNotSelect = paperDao.findAllTopicNotSelect(paperId);

        paperObjectList.addAll(allTopicNotSelect);
        if (paperObjectList != null) {
            return new ResponseEntity(200,"查询成功",paperObjectList);
        }
        return ResponseEntity.FAIL;
    }

    /*删除所选的题*/
    @Override
    public ResponseEntity deleteTopic() {
        int newPaperId = paperDao.findNewId();
        int i=paperDao.deleteTopic(newPaperId);
        if (i > 0) {
            return new ResponseEntity(200, "删除成功",null);
        }
        return new ResponseEntity(500, "删除失败",null);
    }

    /*批量删除*/
    @Override
    public ResponseEntity deleteTopicList(PaperObject paperObject) {
        int i=paperDao.deleteTopicList(paperObject);
        if (i > 0) {
            return new ResponseEntity(200, "批量删除成功",null);
        }
        return new ResponseEntity(500, "批量删除失败",null);
    }

    /*通过设置题目分数用试卷id以及题目类型*/
    @Override
    public ResponseEntity updateTopic(PaperObject paperObject) {

        int i= paperDao.updateTopicById(paperObject);
       if (i > 0) {
           return new ResponseEntity(200, "修改分数成功",null);
       }
        return new ResponseEntity(500, "修改分数失败",null);
    }
//        查询最新试卷id
    @Override
    public ResponseEntity findAddPaperId() {
        int newPaperId = paperDao.findNewId();
        if (newPaperId >0) {
            return new ResponseEntity(200, "查询成功",newPaperId);
        }

        return ResponseEntity.FAIL;
    }




}
